import * as vscode from 'vscode';
import { ChatPanel } from '../ui/index';
import apiIndex from '../api/index';

export function activate(context: vscode.ExtensionContext) {
  let disposable = vscode.commands.registerCommand('hephaestus-ai-assistant.openChatPanel', () => {
    ChatPanel.createOrShow();

    const panel = ChatPanel.currentPanel?.panel;
    if (panel) {
      panel.webview.onDidReceiveMessage(async message => {
        if (message.command === 'sendPrompt') {
          const prompt = message.prompt;
          const promptHistory = message.promptHistory;
          const response = await apiIndex.callDeepSeekAPI(prompt, promptHistory);
          console.log('response: ', response);
          if (response) {
            const jsonResponse = JSON.parse(response);
            const text = jsonResponse.choices[0]['message']['content'];
            panel.webview.postMessage({
              command: 'showResponse',
              response: {
                id: message.id,
                content: { text },
                response: jsonResponse,
              }
            });
          }
        }
      }, undefined, context.subscriptions);
    }
  });

  context.subscriptions.push(disposable);
}